package com.weimob.mcloud.wechatwork.customer.third.customer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.core.adapter.enums.uc.UCSourceTypeEnum;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.onecrm.request.AccountGetDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.onecrm.response.AccountGetBO;
import com.weimob.mcloud.wechatwork.core.adapter.service.OneCrmAdapterService;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.CustomerEmployeeRelation;
import com.weimob.mcloud.wechatwork.core.entity.relation.employee.Employee;
import com.weimob.mcloud.wechatwork.core.enums.customer.CustomerStatusEnum;
import com.weimob.mcloud.wechatwork.core.enums.employee.EmployeeStatusEnum;
import com.weimob.mcloud.wechatwork.core.service.customer.CustomerService;
import com.weimob.mcloud.wechatwork.customer.externalDependencyProxy.usermanagement.EmployeeProxy;
import com.weimob.mcloud.wechatwork.customer.third.core.CustomerServiceProxy;
import com.weimob.soa.common.exception.SoaException;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/12/27 10:47 上午
 */
@Component
@Slf4j
public class CustomerThird {

    @Reference
    private OneCrmAdapterService oneCrmAdapterService;

    @Autowired
    private EmployeeProxy employeeProxy;

    @Reference
    private CustomerService customerService;

    @Autowired
    private CustomerServiceProxy customerServiceProxy;

    public Long getSuperWid(Long bosId, String externalUserId, String corpId) {
        if (StringUtils.isBlank(externalUserId) || Objects.isNull(bosId) || StringUtils.isBlank(corpId)) {
            return null;
        }

        //1、获取外部联系人wid
        AccountGetDTO accountGetDTO = new AccountGetDTO();
        accountGetDTO.setBosId(bosId);
        accountGetDTO.setOpenId(externalUserId);
        accountGetDTO.setUcSourceTypeEnum(UCSourceTypeEnum.WORK_WEIXIN_EXTERNAL_USERID);
        accountGetDTO.setAppId(corpId);
        try {
            SoaResponse<AccountGetBO, ?> accountGetBOSoaResponse = oneCrmAdapterService.getAccountByOpenId(accountGetDTO);
            if (0L != Long.parseLong(accountGetBOSoaResponse.getReturnCode())) {
                log.error("根据externalUserId查询失败, request:{}, response:{}", JSON.toJSONString(accountGetDTO),
                        JSON.toJSONString(accountGetBOSoaResponse));
                return null;
            }

            AccountGetBO userChannelInfoDTO = accountGetBOSoaResponse.getResponseVo();
            if (Objects.isNull(userChannelInfoDTO) || Objects.isNull(userChannelInfoDTO.getWid())) {
                log.info("获取渠道用户信息失败, accountGetDTO:{}", accountGetDTO);
                return null;
            }

            return userChannelInfoDTO.getWid();
        } catch (SoaException e) {
            log.error("获取渠道用户信息失败, request:{}, error:{}", JSON.toJSONString(accountGetDTO), e);
            return null;
        }
    }


    public Employee queryEmployee(PrivateSphere privateSphere, String orgUserId, String txUserId) {
        return employeeProxy.fetchEmployeeById(privateSphere, txUserId, orgUserId);
    }

    public List<Employee> getEffectiveRelationList(PrivateSphere privateSphere, String customerId) {
        List<CustomerEmployeeRelation> dataList;
        try {
            dataList = customerServiceProxy.fetchCustomerEmployeeRelation(customerId,null,privateSphere, CustomerStatusEnum.DEFAULT,true,null);
        } catch (Exception e) {
            log.error("getEffectiveRelationList e,error", e);
            return new ArrayList<>();
        }

        List<Employee> employeeList = new ArrayList<>();
        for (CustomerEmployeeRelation customerEmployeeRelation1 : dataList) {

            Employee employee = customerEmployeeRelation1.getEmployee();

            if (null == employee) {
                continue;
            }

            if (EmployeeStatusEnum.QUITED == employee.getStatus()) {
                continue;
            }

            if (StringUtils.isNotBlank(employee.getTxUserId())) {
                employeeList.add(employee);
            }
        }

        return employeeList;
    }
}
